import os
Import('env')
game_proto_env = env.Clone()

srcroot = game_proto_env['srcroot']
objroot = game_proto_env['objroot']

game_proto_env.Append(CPPPATH = [
                '$objroot/proto',
                '$objroot/proto/net_proto',
                '$objroot/proto/data_proto',
                ])

proto_files = []

for protodir in ['proto/net_proto', 'proto/data_proto', 'proto']:
  auto_proto_dir = os.path.join(srcroot, protodir)
  for dirpath, dirnames, filenames in os.walk(auto_proto_dir):
    for filename in filenames:
      if filename.endswith('.proto'):
        filepath = os.path.join(dirpath, filename)
        proto_files.append(filepath)

for i in proto_files:
  game_proto_env.ProtocolBuffer(i)

def build_init():
  objroot_proto_dirs = [
      os.path.join(game_proto_env["objroot"], 'proto/data_proto'),
      os.path.join(game_proto_env["objroot"], 'proto/net_proto'),
      ]
  for dir in objroot_proto_dirs:
    os.system("mkdir -p " + dir)
  walk_dir = os.path.join(game_proto_env["objroot"], "proto")
  for dir, dirname, files in os.walk(walk_dir):
    os.system("touch " + os.path.join(dir , "__init__.py"))
 
build_init()

proto_src_files = []
proto_obj_files = []
for i in proto_files:
  proto_src_files.append(i.replace('.proto', '.pb.cc').replace(srcroot, objroot))

#game_proto_env.StaticLibrary('game_proto', proto_src_files)
for src in proto_src_files:
  proto_obj_files.append(game_proto_env.Object(src))
game_proto_env.StaticLibrary('game_proto', proto_obj_files)

